﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Navigation;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;

namespace Tsina
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OAuthHelper.OpenAuthorizeUrl();
            button1.IsEnabled = false;
            textBox1.IsEnabled = false;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey("dataSaved") == false)
            {
                PhoneApplicationService.Current.State["appKey"] = OAuthHelper.appKey;
                PhoneApplicationService.Current.State["appSecret"] = OAuthHelper.appSecret;
                PhoneApplicationService.Current.State["requestTokenUrl"] = OAuthHelper.requestTokenUrl;
                PhoneApplicationService.Current.State["authorizeUrl"] = OAuthHelper.authorizeUrl;
                PhoneApplicationService.Current.State["accessTokenUrl"] = OAuthHelper.accessTokenUrl;
                PhoneApplicationService.Current.State["oauthToken"] = OAuthHelper.requestToken;
                PhoneApplicationService.Current.State["oauthTokenSecret"] = OAuthHelper.requestTokenSecret;
                PhoneApplicationService.Current.State["accessToken"] = OAuthHelper.accessToken;
                PhoneApplicationService.Current.State["accessTokenSecret"] = OAuthHelper.accessTokenSecret;
                PhoneApplicationService.Current.State["userId"] = OAuthHelper.userId;

                PhoneApplicationService.Current.State["dataSaved"] = null;
            }

            base.OnNavigatedFrom(e);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey("dataSaved") == true)
            {
                OAuthHelper.appKey = (string)PhoneApplicationService.Current.State["appKey"];
                OAuthHelper.appSecret = (string)PhoneApplicationService.Current.State["appSecret"];
                OAuthHelper.requestTokenUrl = (string)PhoneApplicationService.Current.State["requestTokenUrl"];
                OAuthHelper.authorizeUrl = (string)PhoneApplicationService.Current.State["authorizeUrl"];
                OAuthHelper.accessTokenUrl = (string)PhoneApplicationService.Current.State["accessTokenUrl"];
                OAuthHelper.requestToken = (string)PhoneApplicationService.Current.State["oauthToken"];
                OAuthHelper.requestTokenSecret = (string)PhoneApplicationService.Current.State["oauthTokenSecret"];
                OAuthHelper.accessToken = (string)PhoneApplicationService.Current.State["accessToken"];
                OAuthHelper.accessTokenSecret = (string)PhoneApplicationService.Current.State["accessTokenSecret"];
                OAuthHelper.userId = (string)PhoneApplicationService.Current.State["userId"];
            }
            base.OnNavigatedTo(e);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            OAuthHelper.GetAccessToken(textBox1.Text);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //OAuthHelper.GetUserInfo();
            OAuthHelper.SendMessage(textBox1.Text);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings["zzz"] = "Bill";
            settings.Save();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("zzz") == true)
            {
                textBox1.Text = (string)settings["zzz"];
            }
            else
            {
                textBox1.Text = "none";
            }
        }
    }
}